<template>
	<!-- 应收款 -->
	<el-card>
		<el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList">
			<view v-if="props.type !== 'accountDetail'">
				<el-row>
					<el-col :span="12">
						<el-form-item label="Receipts(self-number) 收款事项(自编号)">
							<el-input v-model="state.queryForm.receiptItemsSelfNumber" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="ReceiptItems(box number) 收款事项(箱号)">
							<el-input v-model="state.queryForm.receiptItemsCntNo" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label="ReceiptItems(receipt number) 收款事项(收据单号)">
							<el-input v-model="state.queryForm.receiptItemsReceiptNumber" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="CollectionMatters(Customers) 收款事项(客户)">
							<el-input v-model="state.queryForm.receiptItemsCustomerName" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				
			</view>
			<el-row style="justify-content: flex-end;">
				<el-form-item>
					<el-button @click="getDataList">Query查询</el-button>
				</el-form-item>
				<el-form-item v-if="props.type !== 'detail'">
					<el-button type="primary" @click="addOrUpdateHandle()">Add新增</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="danger" @click="deleteBatchHandle()">Delete删除</el-button>
				</el-form-item>
			</el-row>
			
		</el-form>
		<el-table
			v-loading="state.dataListLoading"
			:data="state.dataList"
			border
			style="width: 100%"
			@selection-change="selectionChangeHandle"
			@sort-change="sortChangeHandle"
		>
			<el-table-column type="selection" label="序号" header-align="center" align="center" width="50"></el-table-column>
			<el-table-column prop="reversalAmount" label="ReversalAmount 冲销金额" header-align="center" align="center"> </el-table-column>
			<el-table-column prop="currency" label="currency 币种" header-align="center" align="center"></el-table-column>
			<el-table-column prop="receiptItemsSelfNumber" label="Receipts (self-number)收款事项(自编号)" header-align="center" align="center"></el-table-column>
			<el-table-column prop="receiptItemsCntNo" label="Receipt items (box number)收款事项(箱号)" header-align="center" align="center"></el-table-column>
			<el-table-column prop="receiptItemsReceiptNumber" label="Receipt items (receipt number)收款事项(收据单号)" header-align="center" align="center"></el-table-column>
			<el-table-column prop="receiptItemsCustomerName" label="Collection Matters (Customers)收款事项(客户)" header-align="center" align="center"></el-table-column>
			<el-table-column prop="currencyPay" label="Currency of payment收款币种" header-align="center" align="center"></el-table-column>
			<el-table-column prop="confirmReceivedAmount" label="Confirm the amount received确认到账金额" header-align="center" align="center"></el-table-column>
			<el-table-column prop="bankAccount" label="bank account到账银行" header-align="center" align="center"></el-table-column>
			<el-table-column prop="arrivalDate" label="Arrival date到账日期" header-align="center" align="center"  :formatter="arrivalDateFomat"></el-table-column>
			<el-table-column prop="remark" label="Remark备注" header-align="center" align="center"></el-table-column>

			<el-table-column label="Operation 操作" fixed="right" header-align="center" align="center" width="170">
				<template #default="scope">
					<el-button type="primary" link @click="addOrUpdateHandle(scope.row.id)">Update修改</el-button>
					<el-button type="primary" link @click="deleteBatchHandle(scope.row.id)">Delete删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
			:current-page="state.page"
			:page-sizes="state.pageSizes"
			:page-size="state.limit"
			:total="state.total"
			layout="total, sizes, prev, pager, next, jumper"
			@size-change="sizeChangeHandle"
			@current-change="currentChangeHandle"
		>
		</el-pagination>
		
		<!-- 新增 / 修改 -->
		<add-or-update ref="addOrUpdateRef" :account-id="props.accountId" :customer-info="props.customerInfo" @refreshDataList="getDataList"></add-or-update>
	</el-card>
</template>

<script setup lang="ts">
import { useCrud } from '@/hooks'
import { reactive, ref } from 'vue'
import AddOrUpdate from './detail-or-update.vue'

import { IHooksOptions } from '@/hooks/interface'
const props = defineProps({
	type: {
		type: String,

	},
	accountId: {
		type: String
	},
	customerInfo: {
		type: Object
	}
})

let queryForm = {
	receiptItemsSelfNumber: '',
	receiptItemsCntNo: '',
	receiptItemsReceiptNumber: '',
	receiptItemsCustomerName: '',
	receivablesId: ''
}

// 应收款详情
if(props.type === 'accountDetail'){
	queryForm.receivablesId = props.accountId as string;
}

const state: IHooksOptions = reactive({
	dataListUrl: '/receivePayDetail/page',
	deleteUrl: '/receivePayDetail/delete',
	queryForm: queryForm
})

const arrivalDateFomat = (row, column) => {
	const arrivalDate = row.arrivalDate;
	let temp = '';
	if(arrivalDate && arrivalDate !== ''){
		temp = arrivalDate.split(' ')[0]
	}
	return temp
}

const addOrUpdateRef = ref()
const addOrUpdateHandle = (id?: Number) => {
	addOrUpdateRef.value.init(id)
}


const { getDataList, sizeChangeHandle, selectionChangeHandle, sortChangeHandle, currentChangeHandle, deleteBatchHandle } = useCrud(state)
</script>
